<?php


namespace App\Http\Controllers\Api\v1;


use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Controller;
use Kafka;
class KafkaController extends BaseController
{
    public function Producer(){
        //任务 消息内容，--》3000 详细信息 订单信息
        $value = 'wjy 的的 Kafka 订阅';
        //主题
        $topic='test';
        $config = Kafka\ProducerConfig::getInstance();
        $config->setMetadataRefreshIntervalMs(10000);
        $config->setMetadataBrokerList('127.0.0.1:9092');
        $config->setBrokerVersion('1.0.0');
        //消息可靠
        $config->setRequiredAck('1');
        //客户端生产信息的模式 同步 异步 发后既忘
        $config->setIsAsyn(false);//异步
        $config->setProduceInterval(500);
        $producer = new Kafka\Producer(function () use ($value,$topic){
            return [
              [
                  'topic'=>$topic,
                  'value'=>$value,
                  'key'=>'',
                  'partId'=>4
              ],
            ];
        });
        dump($producer);
        $producer->success(function ($result){
            dump($result);
            return $this->witejson(0,$result,'kafka通知成功');
        });
        $producer->error(function ($errorCode){
            var_dump($errorCode);
            return $this->witejson(1,$errorCode,'kafka通知失败');
        });
        dump($producer);

        $producer->send(true);
    }
}
